﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASBCC.Workflow.K2Manager;
using ASBCC.Database.Entity;

namespace ASBCC.Workflow.Client.Controllers
{
    public class IQWorkflowController : WorkflowControllerBase
    {
        private IAppForm _appform ;
        public IAppForm Appform
        {
            get
            {
                return _appform;
            }
        }
        public IQWorkflowController(IWorkflowPage workflowPage, IAppForm appForm, MyActivityTableEntity myActivity)
            : base(workflowPage, appForm, myActivity)
        {
            _appform =appForm;
        }

        public override void BuildAuditDestinationArgument(string actionType)
        {
            string auditDestination = string.Empty;
            string nextActivityName = GetNextActivityName();
            Flow_ProcessConfigTableEntity processConfig = K2DataHelper.GetProcessConfig(GetProcName(), nextActivityName);
            if (processConfig.AuditUserSource != "Customization")
            {
                base.BuildAuditDestinationArgument(actionType);
                return;

            }

            Dictionary<string, object> formArguments = GetFormArguments();
            int[] destination ;
            switch (nextActivityName)
            {
                case IQActivityNameConsts.ApproveBStatus:
                    destination = (int[])(formArguments[IQActivityNameConsts.ApproveBStatus]);
                    break;
                case IQActivityNameConsts.IQReview:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQReview]);
                    break;
                case IQActivityNameConsts.IQCompReceiving:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQCompReceiving]);
                    break;
                case IQActivityNameConsts.IQPrepare:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQPrepare]);
                    break;
                case IQActivityNameConsts.IQCompRecReport:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQCompRecReport]);
                    break;
                case IQActivityNameConsts.IQCompRecRptReview:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQCompRecRptReview]);
                    break;
                case IQActivityNameConsts.IQManufacturePlan:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQManufacturePlan]);
                    break;
                case IQActivityNameConsts.IQAssemblyReport:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQAssemblyReport]);
                    break;
                case IQActivityNameConsts.IQAssemblyRptReivew:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQAssemblyRptReivew]);
                    break;
                case IQActivityNameConsts.IQTestingReport:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQTestingReport]);
                    break;
                case IQActivityNameConsts.IQTestingRptReview:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQTestingRptReview]);
                    break;
                case IQActivityNameConsts.IQReport:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQReport]);
                    break;
                case IQActivityNameConsts.IQReportReview:
                    destination = (int[])(formArguments[IQActivityNameConsts.IQReportReview]);
                    break;
                default:
                    base.BuildAuditDestinationArgument(actionType);
                    return;
            }
            if (destination == null || destination.Length==0)
            {
                throw new Exception(" AuditDestination  is null!");
            }
            K2AuditDestination k2AuditDestination = new K2AuditDestination(nextActivityName, destination );
            auditDestination = k2AuditDestination.ExportText();
            formArguments[WorkflowKeyConsts.AuditDestination] = auditDestination;
        }
       
        public override void BuildK2DataFields(Dictionary<string, object> dataFields)
        {
            base.BuildK2DataFields(dataFields);
            string actionType = GetActionType();
            Dictionary<string, object> formArguments = GetFormArguments();
            dataFields[IQWorkflowKeyConsts.Applicant] = formArguments[IQWorkflowKeyConsts.Applicant];
            dataFields[IQWorkflowKeyConsts.ISBSS] = formArguments[IQWorkflowKeyConsts.ISBSS];
            dataFields[IQWorkflowKeyConsts.TQAssembly] = formArguments[IQWorkflowKeyConsts.TQAssembly];
        }

        public override string GetNextActivityName()
        {
            ActivityLink activityLink = GetActivityLink();
            if (activityLink == null) return string.Empty;
            string currentActivityName = string.Empty;
            currentActivityName = GetCurrentActivityName();
            if ((IsDraft() || IsSubmitted()) && string.IsNullOrEmpty(currentActivityName))
            {
                currentActivityName = GetSubmissionActivityName();
            }
            ActivityLinkNode currentNode = activityLink.FindNode(currentActivityName);
            if (currentNode == null) return string.Empty;
            string actionType = GetActionType();

            if (actionType == K2ActionType.Back)
            {
                string[] activityNames = GetActivityNames();
                int index =0;
                for (int i = 0; i < activityNames.Length-1; i++)
                {
                    if (activityNames[i] == currentNode.Value)
                    {
                        index = i;
                    }
                }
                return  activityNames[index-1];
            }
            else if (actionType == K2ActionType.Cancel)
            {
                return ActivityNameConsts.End;
            }
            else
            {
                ActivityLinkNode lastChild = currentNode.LastChild;
                if (lastChild == null) return string.Empty;
                return lastChild.Value;
            }

        }
        public override ActivityLink GetActivityLink()
        {
            ActivityLink activityLink = new ActivityLink(GetActivityNames());
            return activityLink;
        }
        public string[] GetActivityNames()
        {
            Dictionary<string, object> formArguments = GetFormArguments();

            List<string> activityNames = new List<string>();
            activityNames.Add(ActivityNameConsts.Submission);
            activityNames.Add(IQActivityNameConsts.ApproveBStatus);
            activityNames.Add(IQActivityNameConsts.IQReview);
            activityNames.Add(IQActivityNameConsts.IQCompReceiving);
            activityNames.Add(IQActivityNameConsts.IQPrepare);
            activityNames.Add(IQActivityNameConsts.IQCompRecReport);
            activityNames.Add(IQActivityNameConsts.IQCompRecRptReview);
            activityNames.Add(IQActivityNameConsts.IQManufacturePlan);
            if (!(bool)formArguments[IQWorkflowKeyConsts.ISBSS])
            {
                activityNames.Add(IQActivityNameConsts.IQAssemblyReport);
                activityNames.Add(IQActivityNameConsts.IQAssemblyRptReivew);
                if (!(bool)formArguments[IQWorkflowKeyConsts.TQAssembly])
                {
                    activityNames.Add(IQActivityNameConsts.IQTestingReport);
                    activityNames.Add(IQActivityNameConsts.IQTestingRptReview);
                }
            }
            activityNames.Add(IQActivityNameConsts.IQReport);
            if (!(bool)formArguments[IQWorkflowKeyConsts.ISBSS])
            {
                activityNames.Add(IQActivityNameConsts.IQReportReview);
            }
            activityNames.Add(ActivityNameConsts.End);
            return activityNames.ToArray();

        }
    }

    public class IQWorkflowKeyConsts
    {
        public const string Applicant = "Applicant";
        public const string TQAssembly = "TQAssembly";
        public const string ISBSS = "ISBSS";
    }

    public class IQActivityNameConsts
    {
        public const string ApproveBStatus = "ApproveBStatus";
        public const string IQReview = "IQReview";
        public const string IQCompReceiving = "IQCompReceiving";
        public const string IQPrepare = "IQPrepare";
        public const string IQCompRecReport = "IQCompRecReport";
        public const string IQCompRecRptReview = "IQCompRecRptReview";
        public const string IQManufacturePlan = "IQManufacturePlan";
        public const string IQAssemblyReport = "IQAssemblyReport";
        public const string IQAssemblyRptReivew = "IQAssemblyRptReivew";
        public const string IQTestingReport = "IQTestingReport";
        public const string IQTestingRptReview = "IQTestingRptReview";
        public const string IQReport = "IQReport";
        public const string IQReportReview = "IQReportReview";
    }
}

